import $ from 'jquery'
import { request } from './request'

class RewriteButtonMenu {
    constructor() {
        this.title = '改写'
        this.tag = 'button'
        this.showDropPanel = true
    }

    getValue(editor) {
        return ' hello '
    }

    isActive(editor) {
        return false
    }

    isDisabled(editor) {
        if(editor.getSelectionText()) return false
        return true
    }

    exec(editor, value) {
       
    }

    getPanelContentElem(editor) {
        const $list = $(`<div class="panel-list flex flex-col">
            <div class="panel-item">正式得体</div>
            <div class="panel-item">严肃庄重</div>
            <div class="panel-item">有说服力</div>
            <div class="panel-item">轻松</div>
            <div class="panel-item">热情</div>
        </div>`)

        const selectionText = editor.getSelectionText().trim()

        $list.on('click', '.panel-item', async function () {
            const itemText = this.innerHTML
            const subjectText = `##要求：\n 请帮我将下面这段文字用【${itemText}】的语气进行改写，要求不改变原本的文意。\n ##输入：\n ${selectionText}`

            editor.insertText(selectionText)

            const node = { type: 'paragraph', children: [{ text: '' }] }
            editor.insertNode(node)

            const responseText = await request(subjectText, 4, (text) => {
                editor.insertText(text)
            })
    
            console.log(responseText)
        })

        return $list[0]
    }
}

export const Menu = RewriteButtonMenu

export default {
    key: 'ai-rewriter',
    factory() {
      return new RewriteButtonMenu()
    }
}